const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const passport = require('passport')
const app = express()

// 引入 users
const users = require('./routes/api/users')
// 引入 profile
const profiles = require('./routes/api/profiles')

// DB config
const db = require("./config/keys").mongoURI

// 使用 body-parser 中间件
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

// 使用 passport
app.use(passport.initialize());

require('./config/passport')(passport)

// Connect to mongodb
mongoose.connect(db)
.then(()=> console.log('MongoDB Conneted'))
.catch(err => console.log(err))



// 使用routes
app.use("/api/users", users)
app.use("/api/profiles", profiles)

const port = process.env.PORT || 5000

app.listen(port, ()=> {
    console.log(`Server running on port ${port}`)
})